package cn.getech.data.metadata.controller;

import cn.getech.data.intelligence.common.utils.*;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.data.metadata.constant.ConnectTypeEnum;
import cn.getech.data.metadata.constant.WeekEnum;
import cn.getech.data.metadata.handler.RegisterHandler;
import cn.getech.data.metadata.model.req.collectiontask.CollectionTaskAutoSaveReq;
import cn.getech.data.metadata.model.req.collectiontask.CollectionTaskAutoUpdateReq;
import cn.getech.data.metadata.model.req.collectiontask.CollectionTaskRunReq;
import cn.getech.data.metadata.model.req.meta.CollectionTaskListReq;
import cn.getech.data.metadata.model.res.CollectionTaskAutoInfoRes;
import cn.getech.data.metadata.model.res.CollectionTaskListRes;
import cn.getech.data.metadata.service.CollectionTask2Service;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value="采集任务",tags={"采集任务"})
@RestController
@RequestMapping("md/task2")
public class CollectionTask2Controller {
    @Autowired
    private CollectionTask2Service collectionTask2Service;

    @Autowired
    private RegisterHandler registerHandler;


    @ApiOperation("采集任务列表")
    @PostMapping("/list")
    public PageRes<PageUtils2<CollectionTaskListRes>> list(@RequestBody CollectionTaskListReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils2<CollectionTaskListRes> page = collectionTask2Service.listBy(req);

        return BaseRes.okWithPage(page);
    }

    @ApiOperation("采集任务用户列表")
    @PostMapping("/userList")
    public R userList(){
        List<Map<String,Object>> list = collectionTask2Service.userListBy();
        return R.okWithData(list);
    }

    @ApiOperation("获取数据源类型")
    @PostMapping("/getSourceType")
    public DataRes<List<NameValue<Integer>>> getSourceType() {
        List<NameValue<Integer>> names = ConnectTypeEnum.getList();

        return BaseRes.okWithData(names);
    }

    @ApiOperation("获取周数组")
    @PostMapping("/getWeek")
    public DataRes<List<NameValue<Integer>>> getWeek() {
        List<NameValue<Integer>> names = WeekEnum.getList();

        return BaseRes.okWithData(names);
    }

    @ApiOperation("自动采集任务详情")
    @PostMapping("/info")
    @SysLog(value="自动采集任务详情",type= OperationTypeEnum.GETCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public DataRes<CollectionTaskAutoInfoRes> info(@RequestBody CollectionTaskRunReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);

        CollectionTaskAutoInfoRes res = collectionTask2Service.info(req);

        return BaseRes.okWithData(res);
    }

    @ApiOperation("新增自动采集定时运行的任务")
    @PostMapping("/save")
    @SysLog(value="新增自动采集定时运行的任务",type= OperationTypeEnum.ADDCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public BaseRes save(@RequestBody CollectionTaskAutoSaveReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);

        collectionTask2Service.saveAutoTask(req);

        return BaseRes.ok();
    }

    @ApiOperation("修改自动采集定时运行的任务")
    @PostMapping("/update")
    @SysLog(value="修改自动采集定时运行的任务",type= OperationTypeEnum.UPDATECODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public BaseRes update(@RequestBody CollectionTaskAutoUpdateReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);

        collectionTask2Service.updateAutoTask(req);
        return BaseRes.ok();
    }

    @ApiOperation("自动采集运行")
    @PostMapping("/run")
    @SysLog(value="自动采集运行",type= OperationTypeEnum.RUNCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)

    public BaseRes run(@RequestBody CollectionTaskRunReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);
        collectionTask2Service.run(req);

        return BaseRes.ok();
    }

    @ApiOperation("自动采集停止")
    @PostMapping("/stop")
    @SysLog(value="自动采集停止",type= OperationTypeEnum.STOPCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public BaseRes stop(@RequestBody CollectionTaskRunReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);
        collectionTask2Service.stop(req);

        return BaseRes.ok();
    }

    @ApiOperation("启用或者禁用")
    @PostMapping("/enableOrDisable")
    @SysLog(value="自动采集启用或者禁用",type= OperationTypeEnum.OPENCLOSECODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public BaseRes enableOrDisable(@RequestBody CollectionTaskRunReq req) {
        ValidatorUtils.validateEntity(req, AddGroup.class);
        collectionTask2Service.enableOrDisable(req);

        return BaseRes.ok();
    }

    /**
    * @Description 触发hive库数据到元数据
    * @Author  chengweiping
    * @Date   2020/12/30 15:41
    */
    @GetMapping("/hive/metadata")
    @SysLog(value="触发hive库数据到元数据",type= OperationTypeEnum.ADDCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public BaseRes  buildMetadata() {//   md/task2/hive/metadata
        registerHandler.register(null);
        return BaseRes.ok();
    }

    /**
     * @Description 触发hive库数据到元数据
     * @Author  chengweiping
     * @Date   2020/12/30 15:41
     */
    @GetMapping("/metadata/syn")   //md/task2/metadata/syn
    public BaseRes  sys() {//   md/task2/hive/metadata
        registerHandler.init(null);
        return BaseRes.ok();
    }
}
